package com.hotel.homehotel.before.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.homehotel.before.entiy.Room;
import com.hotel.homehotel.before.entiy.UserHobby;
import com.hotel.homehotel.before.entiy.vo.RoomVo;
import com.hotel.homehotel.before.mapper.UserHobbyMapper;
import com.hotel.homehotel.before.service.OrderService;
import com.hotel.homehotel.before.service.RoomService;
import com.hotel.homehotel.before.service.UserHobbyService;
import com.hotel.homehotel.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags="房间管理" ,value="房间管理")
@RequestMapping("/room")
@CrossOrigin
public class RoomController {
   @Autowired
   RoomService roomService;
   @Autowired
    OrderService orderService;
   @Autowired
    UserHobbyMapper  userHobbyMapper;
   @Autowired
    UserHobbyService userHobbyService;

    @ApiOperation(value = "获取全部房间信息(根据用户id来推荐)")
    @GetMapping("/getAllRoom/{uid}")
    public R getAllRoom(@ApiParam(name = "uid", value = "用户id", required =false)
                            @PathVariable  String uid){
        //验证用户
        System.out.println("kkkkkkkk"+uid);
        List<Room> allRoom = roomService.getAllRoom(uid);

        return  R.define(true,200,"查询成功").data("rooms",allRoom);
    }

    @ApiOperation(value = "通过房间关键字查询房间信息")
    @GetMapping("/getRoomByIByRoomName/{name}/{uid}")
    public R getRoomByIByRoomName(@ApiParam(name = "name", value = "房间名", required = true)
                         @PathVariable  String name,@ApiParam(name = "uid", value = "用户id", required = false)
    @PathVariable  String uid){
        return  R.define(true,200,"查询成功").data("rooms",roomService.getRoomByIByRoomName(name,uid));
    }

    @ApiOperation(value = "通过房间id获取房间信息")
    @GetMapping("/getRoomById/{rid}/{uid}")
    public R getRoomById(@ApiParam(name = "rid", value = "房间id", required = true)
                            @PathVariable  String rid,@ApiParam(name = "uid", value = "用户id", required =false)
    @PathVariable  String uid){

        List<RoomVo> roomTime = orderService.selectTime(rid);

        return  R.define(true,200,"查询成功").data("room",roomService.getRoomById(rid)).data("roomTimes",roomTime);
    }

    @ApiOperation(value = "添加房间")
    @PostMapping("/insertRoom")
    public  R insertRoom(@RequestBody  Room room){
        boolean bool= roomService.insertRoom(room);
        if(bool)
            return  R.define(true,200,"添加成功");
        return  R.define(false,400,"添加失败");
    }
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "修改房间信息")
    @PostMapping("/updateRoom")
    public R updateRoom(@RequestBody  Room room){

        boolean bool= roomService.updateRoom(room);
        if(bool)
            return  R.define(true,200,"修改成功");
        return  R.define(false,400,"修改失败");
    }
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "删除房间")
    @GetMapping("/deleteRoom/{id}")
    public R deleteRoom(@ApiParam(name = "id", value = "房间id", required = true)
                            @PathVariable  String id) {
        boolean bool= roomService.deleteRoom(id);
        if(bool)
            return  R.define(true,200,"删除成功");
        return  R.define(false,400,"删除失败");
    }
    }




